package com.common.UITags;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.bean.SelectItem;
import com.common.GlobalPara;


/**
 * 下拉框，支持自动从字典表里获取数据
 * 
 * @author zhanglong
 * 
 */
public class DropDownTag extends TagSupport {
	private static final long serialVersionUID = 1L;

	private StringBuffer sBF = new StringBuffer();
	private StringBuffer sBFl = new StringBuffer();
	JspWriter out;
	private String id;// id
	private String name;// 名字
	private String code;// 代码值
	private String defaultselect;//默认选中的值
	private String editable = "false";//是否可以编辑
	private String label;//下拉框前边的文本
	private String dsCode;//就是自定义的数据源，在进入页面之前该数据源就已经准备好了，并且放到了requet对象里面
	private String width;//宽度
	private String required="false";//是否必录项
	private String readOnly = "false";

	@Override
	public int doStartTag() throws JspException {
		try {
			LinkedHashMap<String, List<SelectItem>> codeList;
			List<SelectItem> codetemp = new ArrayList<SelectItem>();

			out = pageContext.getOut();
			sBFl.setLength(0);
			if(!(label == null || "".equals(label))){
				sBFl.append("<span style=\"font-size: small;vertical-align: middle;font-weight: bold;color:#535C5E;display:inline-block;width:43%;text-align:right;\">" +label);
				//如果为必录项则加上一个红色的星号
				if(required!=null&&!"".equals(required)&&"true".equals(required)){
					sBFl.append("<font color=\"red\">*</font>");
				}
				sBFl.append(":&nbsp;&nbsp;</span>");
			}
			sBF.setLength(0);
			sBF.append("<select ");
			if("true".equals(readOnly)){
				sBF.append(" readonly=\"true\" ");
			}
			sBF.append("style=\"");
			if(!(width==null||"".equals(width))){
				sBF.append("width:" + width +";");
			}else if(this.getParent() instanceof FormTableTag){
				sBF.append(" width: 100%; ");//有的版本不支持百分比，有的支持，很是奇怪
			}
			sBF.append("\"");
			sBF.append(" class=\"easyui-combobox\" ");
			sBF.append(" id=\"" + id + "\" ");
			if (name != null) {
				sBF.append(" name=\"" + name + "\" ");
			}else{
				sBF.append(" name = \""+id+"\" ");
			}
			
			if(required!=null&&!"".equals(required)){
				sBF.append(" required=\""+required+"\" ");
			}
			if(editable!=null&&!"".equals(editable)){
				sBF.append(" editable=\""+editable+"\" " );
			}
			sBF.append(" >");
			sBF.append("<option value=\"\"");
			if(defaultselect==null || "".equals(defaultselect)){
				sBF.append(" selected= \"selected\" ");
			}
			sBF.append(">");
			sBF.append("--请选择--");
			sBF.append("</option>");
			
			//begin支持从系统缓存中提取数据，也支持自定义数据源，但是数据源必须放在request中
			if (!(code == null||"".equals(code))) {
				codeList = GlobalPara._codelist;
				codetemp = codeList.get(code.toUpperCase());
			}else if(!(dsCode==null ||"".equals(dsCode))){
				codetemp = (List<SelectItem>)pageContext.getRequest().getAttribute(dsCode);
			}
			
			if(!(code == null||"".equals(code))||!(dsCode==null ||"".equals(dsCode))){
				for(SelectItem item : codetemp) {
					if(item.getValue().equals(defaultselect)){
						sBF.append("<option value=\"" + item.getValue() + "\"");
						sBF.append(" selected= \"selected\" >");
					}else{
						sBF.append("<option value=\"" + item.getValue() + "\">");
					}
					sBF.append(item.getText());
					sBF.append("</option>");
				}
			}
			//end
			
			//如果在formtable里面就加上表格标签
			if(this.getParent() instanceof FormTableTag){
				out.print("<td width=\"33%\"> " + sBFl.toString() +" <div style=\"width:55%;display:inline-block;\"> " + sBF.toString());
			}else{
				out.print(sBF.toString());
			}
		} catch (IOException e) {
			throw new JspException("标签dropdown往前台写入时异常！");
		} catch (Exception e) {
			throw new JspException("标签内部异常！");
		}
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			if(this.getParent() instanceof FormTableTag){
				out.print("</select></div>"+"</td>");
			}else{
				out.print("</select>");
			}
		} catch (IOException e) {
			throw new JspException("标签dropdown往前台写入时异常！");
		} catch (Exception e) {
			throw new JspException("标签内部异常！");
		}
		return EVAL_PAGE;
	}

	/**
	 * 以下为属性的get和set方法
	 */
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getDefaultselect() {
		return defaultselect;
	}

	public void setDefaultselect(String defaultselect) {
		this.defaultselect = defaultselect;
	}

	public String getEditable() {
		return editable;
	}

	public void setEditable(String editable) {
		this.editable = editable;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getDsCode() {
		return dsCode;
	}

	public void setDsCode(String dsCode) {
		this.dsCode = dsCode;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getRequired() {
		return required;
	}

	public void setRequired(String required) {
		this.required = required;
	}

	public String getReadOnly() {
		return readOnly;
	}

	public void setReadOnly(String readOnly) {
		this.readOnly = readOnly;
	}
}
